package HomeWork20241201;

import java.util.Scanner;
/*
* 数值转大写的第一种方式
* */
public class HomeWork1_1 {
    public static void main(String[] args) {
        int num = getNum();

        getBigNum(num);
    }

    /**
     * 将数字转换为大写金额
     *
     * @param num
     */
    public static void getBigNum(int num) {
        String[] bNum = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
        String[] dW = {"", "拾", "佰", "仟"};

        // 如果数字为0，则直接输出"零圆"
        if (num == 0) {
            System.out.println("零圆");
            return;
        }

        StringBuilder result = new StringBuilder();
        int wei = 0;

        // 标记是否已经添加过“零”
        boolean zeroFlag = false;

        while (num > 0) {
            int digit = num % 10;
            num /= 10;

            if (digit != 0) {
                if (zeroFlag) {
                    // 如果之前遇到过零且当前不是零，则插入一个“零”
                    result.insert(0, "零");
                    zeroFlag = false;
                }
                result.insert(0, bNum[digit] + dW[wei]);
            } else {
                zeroFlag = true;
            }
            wei++;
        }

        result.append("圆");

        System.out.println(result.toString());
    }

    /**
     * 获取用户输入的数字
     *
     * @return
     */
    public static int getNum() {
        Scanner sc = new Scanner(System.in);
        String str = null;
        int num = 0;

        // 获取用户输入并验证
        while (true) {
            System.out.println("请输入一个一万以内的正整数:");
            str = sc.nextLine();
            if (isNum(str)) {
                num = Integer.parseInt(str);
                if (num >= 0 && num < 10000) {
                    break;
                } else {
                    System.out.println("您输入的有误,请重新输入!");
                }
            }
        }
        return num;
    }

    /**
     * 判断是否为数字
     *
     * @param str
     * @return
     */
    public static boolean isNum(String str) {
        return str.matches("\\d+");
    }
}
